package com.ruoyi.userInfo.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.userInfo.domain.IaeCarSeries;
import com.ruoyi.userInfo.domain.IaeRegions;
import com.ruoyi.userInfo.mapper.CarSeriesMapper;
import com.ruoyi.userInfo.service.CarSeriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Author: Z.H.Z
 * Date: 2025/2/14 10:51
 */
@Service
public class CarSeriesServiceImpl implements CarSeriesService {

    @Autowired
    private CarSeriesMapper carSeriesMapper;


    @Override
    public AjaxResult carListDto() {

        List<IaeCarSeries> selectList = carSeriesMapper.selectSeriesLists(Wrappers.emptyWrapper());
        //启动流线程
        List<IaeCarSeries> collect = selectList.stream()
                .filter(carSeries -> carSeries.getParentId()==0)
                .map((regionsList -> {
                    IaeCarSeries iaeCarSeries = new IaeCarSeries();
                    iaeCarSeries.setId(regionsList.getId());
                    iaeCarSeries.setCarName(regionsList.getCarName());
                    iaeCarSeries.setChildren(getChildren(selectList,regionsList.getId()));
                    return iaeCarSeries;
                })).collect(Collectors.toList());
        return AjaxResult.success("200",collect);

    }

    private List<IaeCarSeries> getChildren(List<IaeCarSeries> selectList, Integer id) {
        //启动流线程
        List<IaeCarSeries> collect = selectList.stream()
                .filter(typeDTO -> typeDTO.getParentId().equals(id))
                .map((selectListDto -> {
                    IaeCarSeries iaeCarSeries = new IaeCarSeries();
                    iaeCarSeries.setId(selectListDto.getId());
                    iaeCarSeries.setCarName(selectListDto.getCarName());
                    iaeCarSeries.setChildren(getChildren(selectList,selectListDto.getId()));
                    return iaeCarSeries;
                })).collect(Collectors.toList());
        return collect.size()==0?null:collect;
    }
}
